﻿using System.Collections.Generic;
using System.Xml.Linq;

namespace xycn.pluginsupport.ControllerPlugins
{
    public class ControllerConfigure
    {
        private static XElement controllerConfiguration = null;

        //程序运行环境
        public static string RuntimeDir = "";

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="configuration"></param>
        public ControllerConfigure(XElement configuration)
        {
            if (controllerConfiguration == null)
            {
                controllerConfiguration = configuration;
            }
        }

        /// <summary>
        /// 获取指定Controller库的配置项
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static XElement GetControllerConfig(string name)
        {
            XElement ret = null;

            if (controllerConfiguration == null)
                return null;

            if (name == null)
                return controllerConfiguration;

            //获取controller插件位置
            IEnumerable<XElement> controllers =
                controllerConfiguration.Elements();

            //遍历控制器插件配置
            foreach (XElement item in controllers)
            {
                XAttribute controller_attr = item.Attribute("name");
                if (controller_attr == null)
                {
                    continue;
                }
                if (controller_attr.Value == name)
                {
                    ret = item;
                    break;
                }
            }

            return ret;
        }
    }
}
